<script setup>
import MyNav from "@/components/MyNav.vue";
import MyHead from "@/components/MyHead.vue";
import MyTable from "@/components/MyTable.vue";
import {onMounted, ref, shallowReactive, shallowRef} from "vue";
import {dateFormat, myBatchDelete, myRemove, myPage, myRemoveBatch} from "@/util";
import {pageApi, pageLikeTitleApi, removeApi, removeBatchApi} from "@/api/AssetsApi";
import MyPager from "@/components/MyPager.vue";
import MyList from "@/components/MyList.vue";
import router from "@/router";
import MyForm from "@/components/MyForm";
import {excelApi} from "@/api/axios";
import {MINIO_HOST} from "@/const";

// 表格数据
let records = ref();
let record = ref();
let assetsTitle=shallowRef();
// 分页数据
let pageInfo = shallowReactive({'pageNum': 1, 'pageSize': 5, 'fn': page});

/* ==================== 分页查询 ==================== */

//async function page(pageNum = pageInfo['pageNum'],
                  //  pageSize = pageInfo['pageSize']) {

  //await myPage({
  //  'api': pageApi,
   // 'params': {'pageNum': pageNum, 'pageSize': pageSize},
   // 'records': records,
  //  'pageInfo': pageInfo
 // });
//}
async function page(pageNum = pageInfo['pageNum'],
                    pageSize = pageInfo['pageSize']) {
let config = {
  'api':pageApi,
  'params':{'pageNum':pageNum,'pageSize':pageSize},
  'records':records,
  'pageInfo':pageInfo
}
//搜索资产名称
if (assetsTitle.value){
  config['params']['title']=assetsTitle.value;
  config['api']=pageLikeTitleApi;
}
//发送分页请求
await myPage(config);
}

/* ==================== 单条添加 ==================== */
function insert() {
  router.push('/rms/AssetsInsert');
}

/* ==================== 单条修改 ==================== */
function update(record) {
  sessionStorage.setItem('assets',JSON.stringify(record));
  router.push('/rms/AssetsUpdate');
}

/* ==================== 单条删除 ==================== */
async function remove(record) {
  await myRemove({
    'id': record['id'],
    'api': removeApi,
    'fn': page
  });
}


/* ==================== 批量删除 ==================== */
let ids = shallowRef();

async function removeBatch() {
  await myRemoveBatch({
    'ids': ids.value,
    'api': removeBatchApi,
    'fn': page
  });
}

/* ==================== 查看详情 ==================== */
const detailDrawer = shallowRef();
let detailItems = ref();

function detail(record) {
  detailItems.value = [
    {'label': '资产名称', 'value': record['title']},

  {'label': '资产图片', 'value': record['picture'],'type':'img',minio: url => MINIO_HOST + '/test/' + url},
    {'label': '资产价格', 'value': record['price']},
    {'label': '价格单位', 'value': record['priceUnit']},

    {'label': '库存剩余', 'value': record['stock']},
    {'label': '库存单位', 'value': record['stockUnit']},

    {'label': '库存总计', 'value': record['total']},
    {'label': '创建时间', 'value': record['created'], 'format': dateFormat},
    {'label': '修改时间', 'value': record['updated'], 'format': dateFormat},
    {'label': '资产描述', 'value': record['info'], 'type': 'card'},
  ];
  // 打开抽屉
  detailDrawer.value = true;
}

/* ==================== 搜索资产名 ==================== */
function pageByTitle(record) {
  assetsTitle.value=record;
  page();
}

/* ==================== 加载函数 ==================== */
onMounted(() => {
  page();
});

</script>

<template>

  <my-nav :items="[
      {'icon': 'User', 'label': '资产管理'},
      {'icon': 'User', 'label': '资产列表'},
  ]"/>

  <my-head :items="[
    {'type': 'ipt', 'span': 5, 'placeholder': '资产名称', 'fn': pageByTitle},
    {'type': 'insertBtn', 'offset': 16, 'fn': insert},
    {'type': 'deleteBtn', 'fn': removeBatch},
    {'type': 'excelBtn', 'fn':()=> excelApi('/assets/excel','资产统计表.xlsx')},
  ]"/>

  <my-table :records="records" :columns="[
    {'column': '资产名称', 'property': 'title'},
    {'column': '资产图片', 'property': 'picture','type':'img',minio: url => MINIO_HOST + '/test/' + url},
    {'column': '资产价格', 'property': 'price'},
        {'column': '价格单位', 'property': 'priceUnit'},

    {'column': '库存剩余', 'property': 'stock'},
        {'column': '库存单位', 'property': 'stock'},

    {'column': '库存总计', 'property': 'total'},
    {'column': '资产描述', 'property': 'info', 'type': 'card'},
  ]" :buttons="[
    {'type': 'info', 'fn': detail},
    {'type': 'update', 'fn': update},
    {'type': 'delete', 'fn': remove},
  ]" :selection-change="rows => ids = rows.map(e => e['id'])"/>

  <my-pager :page-info="pageInfo"/>

  <el-drawer v-model="detailDrawer" size="30%">
    <my-list title="记录详情" :items="detailItems"></my-list>
  </el-drawer>


</template>

<style scoped lang="scss">

</style>